<template>
    <div class="components-container">
        <!-- 编辑 -->
        <sweet-modal ref="dialog" class="dialog" :sweetModalMaxWidth="700">
            <h3>{{title}} </h3>
            <div>
                <div class="layui-form-item">
                    <!-- <div class="layui-inline">
                        <label class="layui-form-label">员工ID</label>
                        <div class="layui-input-inline">
                            <el-select v-model="regList.SBSSTAID" :disabled="isINFO" placeholder="请选择员工">
                                    <el-option v-for="item in QueryNolstaff" :key="item.label" :label="item.value" :value="item.label">
                                    </el-option>
                            </el-select>
                        </div>
                    </div> -->
                    <div class="layui-inline">
                        <label class="layui-form-label">补贴类型</label>
                        <div class="layui-input-inline">
                            <el-select v-model="regList.SBSCODNAME" :disabled="isINFO" placeholder="请选择补贴类型">
                                <el-option v-for="item in GetSbsType" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">补贴方式</label>
                        <div class="layui-input-inline">
                            <el-select @change="changesbsmode" :disabled="isINFO" v-model="regList.SBSTYPE" placeholder="请选择补贴方式">
                                <el-option v-for="item in GetSbsMode" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">补贴金额</label>
                        <div class="layui-input-inline">
                            <el-input @keyup.native="changesbsmoney" v-model="regList.SBSMONEY" :disabled="isINFO"></el-input>
                        </div>
                    </div>
                </div>
                <!-- <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">经办人</label>
                        <div class="layui-input-inline">
                            <el-input v-model="regList.SBSCHECKUSR"></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">经办日期</label>
                        <div class="layui-input-inline">
                            <el-date-picker v-date v-model="regList.SBSCHECKDATE" type="date" placeholder="选择时间"></el-date-picker>
                        </div>
                    </div>
                </div> -->
            </div>
            <div class="layui-form-item btnCenter">
                <el-button @click="cancels">取消</el-button>
                <el-button type="primary" @click="submit" :loading="this.$store.state.isloading" v-if="this.title !== '补贴设置详情'">提交</el-button>
            </div>
        </sweet-modal>
    </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>


</style>
<script>
    import sweetModal from "@/components/sweetModal";
    import dataTable from "@/components/Table/DataTable";
    import apiBase from "@/api/apiBase.js";
    import {
        formatDate,
        timeStringToDate
    } from "@/utils/date.js";
    import {
        manage
    } from "@/mixins/managePage.js";
    const ADD = "新增";
    const MODIFY = "修改";
    const DELETE = "删除";
    const INFO = "详情";
    const END = "结束使用";
    const BEG = "开始使用";
    const REG = "登记";
    const CONTINUE = "使用续存";
    const TOBESTORED = 'X';
    export default {
        name: "assUseMain",
        mixins: [manage],
        data() {
            return {
                isINFO: true,//详情页面全部不能修改
                GetSbsType: [],
                GetSbsMode: [],
                ids: null,
                QueryNolstaff: [], //获取员工id
                regList: {
                    SBSORGID: null,	   //单位编号	  
                    // SBSSTAID: null,	   //员工ID	  
                    SBSCODNAME: null,   //补贴类型	  
                    SBSTYPE: null, 	   //补贴方式	   A-固定值，B-百分比
                    SBSCHECKUSR: null,  // 经办人	  
                    SBSCHECKDATE: null, //经办日期
                    SBSMONEY: null, // 补贴金额
                },
            }
        },
        methods: {
            open(order,id) {
                this.ids = id;
                this.order = order;
                switch (this.order) {
                    case ADD:
                        this.title = "补贴设置新增";
                        this.dataRepeat();
                        this.isINFO = false;
                        this.$refs.dialog.open();
                        break;
                    case MODIFY:
                        this.title = "补贴设置修改";
                        // this.dataRepeat();
                        this.dataInfo(id);
                        this.isINFO = false;
                        this.$refs.dialog.open();
                        break;
                    case INFO:
                        this.title = "补贴设置详情";
                        this.dataInfo(id);
                        this.isINFO = true;
                        this.$refs.dialog.open();
                        break;
                    default:
                }
            },
            //提交
            submit() {
                this.$store.state.isloading = true;
                switch (this.order) {
                    case ADD:
                        this.dataAdd();
                        break;
                    case MODIFY:
                        this.dataModify();
                        break;
                    default:
                }
            },
            //补贴方式改变
            changesbsmode(val){
                var val = this.regList.SBSTYPE;
                if(val === "A"){//固定值

                }else if(val === "B"){//百分比
                    if(Number(this.regList.SBSMONEY) > 100){
                        this.regList.SBSMONEY = 0;
                    }
                }
            },
            //补贴金额改变
            changesbsmoney(){
                var val = this.regList.SBSMONEY;
                this.regList.SBSMONEY = this.regList.SBSMONEY.replace(/[^\d]/g,"");
                if(this.regList.SBSTYPE === "A"){//固定值
                    if(Number(val) < 0){  
                        this.regList.SBSMONEY = 0;
                    }
                }else if(this.regList.SBSTYPE === "B"){//百分比
                    if(Number(val) < 0){  
                        this.regList.SBSMONEY = 0;
                    }else if(Number(val) > 100){
                        this.regList.SBSMONEY = 100;
                    }
                }else{
                    alert('请选择补贴方式！！');
                }
            },
            //获取补贴类型
            GetSbsTypes() {
                let data = {};
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().Code.GetSbsType,
                    res => {
                        let assTypeData = res.data.content,
                            legendsUsersData = assTypeData.map(item => ({
                                label: item.CodeName,
                                value: item.CodeValue
                            }));
                        this.GetSbsType = legendsUsersData;
                    });
            },
            //获取补贴方式
            GetSbsModes() {
                let data = {};
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().Subsidy.GetSbsType,
                    res => {
                        let assTypeData = res.data.content,
                            legendsUsersData = assTypeData.map(item => ({
                                label: item.Text,
                                value: item.Value
                            }));
                        this.GetSbsMode = legendsUsersData;
                    });
            },
           //获取员工id
            QueryNolstaffs() {
                let data = {};
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().Staff.QueryNolstaff,
                    res => {
                        let assTypeData = res.data.content,
                            legendsUsersData = assTypeData.map(item => ({
                                label: item.STAID,
                                value: item.STANAME
                            }));
                        this.QueryNolstaff = legendsUsersData;
                    });
            },
            //数据重置
            dataRepeat() {
                // this.regList = {};
                Object.assign(this.$data.regList, this.$options.data().regList);
            },
            //新增
            dataAdd() {
                let Data = JSON.parse(JSON.stringify(this.regList));
                // if (Data.ASHBEGDATE)
                //     Data.ASHBEGDATE = formatDate(Data.ASHBEGDATE, "yyyyMMdd");
                // if (Data.ASHENDDATE)
                //     Data.ASHENDDATE = formatDate(Data.ASHENDDATE, "yyyyMMdd");
                apiBase.DoService(
                    Data,
                    apiBase.getApiAdress().Subsidy.CreateSbs,
                    res => {
                        this.refreshData();
                        this.$refs.dialog.close();
                    });
            },
            //详情
            dataInfo(id) {
                var data = {
                    SBSID: id
                };
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().Subsidy.DetailSbs,
                    res => {
                    //    this.regList.SBSSTAID = res.data.content.SBSSTAID;
                       this.regList.SBSCODNAME = res.data.content.SBSCODNAME; 
                       this.regList.SBSTYPE = res.data.content.SBSTYPE; 
                       this.regList.SBSMONEY = res.data.content.SBSMONEY; 
                    });
            },
            //修改
            dataModify() {
                let Data = JSON.parse(JSON.stringify(this.regList));
                Data.SBSID = this.ids;
                apiBase.DoService(
                    Data,
                    apiBase.getApiAdress().Subsidy.ModifySbs,
                    res => {
                        this.refreshData();
                        this.$refs.dialog.close();
                    });
            },
            // 页面关闭
            cancels() {
                this.$refs.dialog.close();
            },
            refreshData() {
                this.$emit("refreshData");
            },
        },
        created() {
            this.GetSbsTypes();
            this.GetSbsModes();
            this.QueryNolstaffs();
        },
        components: {
            sweetModal
        }
    };
</script>

